/******************************************************************************
 * Copyright 2017 The Baidu Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/
package com.hazens.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.widget.Toast;

public class ConnectClient {

    private static final String TAG = "ConnectClient";
    private static final String CONNECT_CLIENT_HANDLER_THREAD_NAME = "ConnectClientHandlerThread";

    private Context mContext = null;

    private Messenger mConnectService = null;
    private Messenger mConnectClient = null;

    private boolean isUsbConnected = false;
    private boolean isConnecting = false;
    private boolean isConnected = false;
    private boolean isBound = false;

    private static ConnectClient mInstance = null;

    private class ConnectClientHandler extends Handler {
        public ConnectClientHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            if (null == msg) {
                return;
            }
            switch (msg.what) {
                case CommonParams.MSG_USB_STATE_MSG:
                    if (msg.arg1 == CommonParams.MSG_USB_STATE_MSG_ON) {
                        isUsbConnected = true;
                        Log.e(TAG, "USB Cable is connected!");
                    } else if (msg.arg1 == CommonParams.MSG_USB_STATE_MSG_OFF) {
                        isUsbConnected = false;
                        Log.e(TAG, "USB Cable is disconnected!");
                    }
                    break;
                case CommonParams.MSG_CONNECT_SERVICE_MSG:
                    if (msg.arg1 == CommonParams.MSG_CONNECT_SERVICE_MSG_START) {

                    } else if (msg.arg1 == CommonParams.MSG_CONNECT_SERVICE_MSG_STOP) {

                    }
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            Log.d(TAG, "onServiceConnected");
            isBound = true;

            mConnectService = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            Log.d(TAG, "onServiceDisconnected");
            isBound = false;
            mConnectService = null;
        }
    };

    public static ConnectClient getInstance() {
        if (null == mInstance) {
            synchronized (ConnectClient.class) {
                if (null == mInstance) {
                    mInstance = new ConnectClient();
                }
            }
        }
        return mInstance;
    }

    private void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

}
